package com.yourcurtain.client;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.yourcurtain.client.mvp.activity.AppActivityMapper;
import com.yourcurtain.client.mvp.place.AppPlaceHistoryMapper;
import com.yourcurtain.client.mvp.place.YourcurtainPlace;
import com.yourcurtain.client.widget.TitlesWidget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Yourcurtain implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * This is the entry point method.
	 */

	private Place yourcurtainPlace = new YourcurtainPlace("World!");
	private SimplePanel appWidget = new SimplePanel();

	public void onModuleLoad() {

		ClientFactory clientFactory = GWT.create(ClientFactory.class);
		EventBus eventBus = clientFactory.getEventBus();
		PlaceController placeController = clientFactory.getPlaceController();

		// Start ActivityManager for the main widget with our ActivityMapper
		ActivityMapper activityMapper = new AppActivityMapper(clientFactory);
		ActivityManager activityManager = new ActivityManager(activityMapper,
				eventBus);
		activityManager.setDisplay(appWidget);
		
		
		// Start PlaceHistoryHandler with our PlaceHistoryMapper
		AppPlaceHistoryMapper historyMapper = GWT
				.create(AppPlaceHistoryMapper.class);
		PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(
				historyMapper);
		historyHandler.register(placeController, eventBus, yourcurtainPlace);
		
		RootPanel.get().add(appWidget);
		// Goes to the place represented on URL else default place
		historyHandler.handleCurrentHistory();

		// HandlerManager eventBus = new HandlerManager(null);

		// AppController appController = new AppController(eventBus, new
		// YourcurtainView());

		// appController.go(RootPanel.get("root"));
		// mainPanel.add(titlesWidget);
		//
		// RootPanel.get("root").add(mainPanel);
		//
		// HistoryManager.clearParameter();
		// HistoryManager.putTitle(HistoryManager.INDEX);
		//
		// History.addValueChangeHandler(new ValueChangeHandler<String>() {
		//
		// @Override
		// public void onValueChange(ValueChangeEvent<String> event) {
		//
		// HashMap<String, String> changeValuesMap = new HashMap<String,
		// String>();
		//
		// String[] changeValues = event.getValue().substring(1)
		// .split(HistoryManager.SYMBOL_SEPARATOR);
		// for (String changeItem : changeValues) {
		// changeValuesMap.put(
		// changeItem.split(HistoryManager.SYMBOL_ASSIGNMENT)[0],
		// changeItem.split(HistoryManager.SYMBOL_ASSIGNMENT)[1]);
		// }
		// System.out.println("changeValuesMap=" + changeValuesMap);
		//
		// if (changeValues.length == 1) {
		// if (changeValuesMap.get(HistoryManager.TITLE) != null) {
		// String titleValue = changeValuesMap
		// .get(HistoryManager.TITLE);
		//
		// if (titleValue.equals(HistoryManager.ANIME)) {
		// titlesWidget.playVideo();
		// }
		// // if (titleValue.equals(HistoryManager.INDEX)) {
		// // titleBarWidget.playVideo();
		// // }
		//
		// }
		// ;
		//
		// }
		// }
		//
		// });

	}
}
